package cn.wzut.edut.controller;

import cn.wzut.edut.dao.pojo.table.PtyClass;
import cn.wzut.edut.pojo.ClassInfo;
import cn.wzut.edut.pojo.ClassModInfo;
import cn.wzut.edut.pojo.JsonResult;
import cn.wzut.edut.service.ClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 班级管理控制层
 */
@RestController
@RequestMapping("/class")
public class ClassController {

    @Autowired
    ClassService classService;

    /**
     * 获取班级列表
     *
     * @return
     */
    @PostMapping("get_list")
    public JsonResult getClassList(HttpServletRequest request){
        String token = request.getHeader("token");
        List<PtyClass> classList = classService.getClassList(token);
        if(classList != null){
            return new JsonResult(classList);
        }
        return new JsonResult(-1, "获取班级列表失败");
    }

    /**
     * 新增班级
     * @return
     */
    @PostMapping("add")
    public JsonResult addClass(HttpServletRequest request, ClassInfo classInfo){
        String token = request.getHeader("token");
        if(classService.addClass(token, classInfo)){
            return new JsonResult(1, "添加班级成功");
        }
        return new JsonResult(-1, "添加班级失败");
    }

    /**
     * 删除班级
     * @return
     */
    @PostMapping("del")
    public JsonResult DelClass(HttpServletRequest request, String[] classIds){
        String token = request.getHeader("token");
        if(classService.delClass(token, classIds)){
            return new JsonResult(1, "删除班级成功");
        }
        return new JsonResult(-1, "删除班级失败");
    }

    /**
     * 修改班级
     * @return
     */
    @PostMapping("mod")
    public JsonResult modClass(HttpServletRequest request, ClassModInfo classModInfo){
        String token = request.getHeader("token");
        if(classService.modClass(token, classModInfo)){
            return new JsonResult(1, "修改班级成功");
        }
        return new JsonResult(-1, "修改班级失败");
    }

    /**
     * 获取班级信息
     * @return
     */
    @PostMapping("get")
    public JsonResult getClassInf(HttpServletRequest request, String classId){
        String token = request.getHeader("token");
        ClassInfo classInfo = classService.getClassById(token, classId);
        if(classInfo != null){
            return new JsonResult(classInfo);
        }
        return new JsonResult(-1, "获取班级信息失败");
    }

}
